
library game;

import 'dart:math';
import 'dart:html' as html;
import 'package:stagexl/stagexl.dart';


// 宣告Titan類別
part 'Titan.dart';
part 'TitanSprite.dart';
part 'GameScene.dart';

// 舞台
Stage gStage;
// 渲染迴圈
RenderLoop gRenderLoop;
// 資源管理
ResourceManager gResourceManager;


// 遊戲場景
GameScene gGameScene;

FlumpLibrary gFlumpLibrary;

bool gbStopCreate;
// 遊戲開始 1 完成過關 2 失敗 3
int gbGameState;

//TextField gGameOverTextField;
// 遊戲開始
SimpleButton gStartButton;
TextField gStartButtonTextField;

// 遊戲結束
//SimpleButton gEndButton;
//TextField gEndButtonTextField;

Bitmap gLogo;
Bitmap gDesktop;
//Bitmap gBackground;

Bitmap gSuccess;
Bitmap gFailed;

// 已產生巨人數
int gTitanCount;
// 擊倒數
int gDefeatCount;


// 巨人數
TextField gTitanCountTextField;

// 結算隻數
TextField gTitanDefeatTextField;
// 結算暱稱
TextField gTitleTextField;

int nScreenWidth = 800;
int nScreenHeight = 600;

// 開發者
TextField gCopyrightTextField;


void main()
{
  
  gbStopCreate = false;
  
  // 檢查是否支援多點觸控
  if (Multitouch.supportsTouchEvents) {
    Multitouch.inputMode = MultitouchInputMode.TOUCH_POINT;
  } else {
//   html.window.alert('不支援多點觸控');
  }
  
  
  // 初始化 Display List
  // 參考：http://www.adobe.com/devnet/flash/quickstart/display_list_programming_as3.html
  gStage = new Stage("gameStage", html.document.query('#gameStage')); 

  gRenderLoop = new RenderLoop();
  gRenderLoop.addStage(gStage);
  
  
  
  // 只是為了加入觸控Listener
//  var glass = new GlassPlate(800,600);
//  glass.addTo(gStage);
//  glass.addEventListener(TouchEvent.TOUCH_BEGIN, onTouchBegin);
  
  
  
  
  

  // Google 開發的新格式 webp (有工具可以轉換)
  // BitmapData.defaultLoadOptions.webp = true;
  
  // 讀取資源
  // Dart語法: ".."是懶人寫法 指的是用相同物件去呼叫方法
  gResourceManager= new ResourceManager()
    ..addBitmapData("buttonUp", "../assets/images/ButtonUp.png")
    ..addBitmapData("buttonOver", "../assets/images/ButtonOver.png")
    ..addBitmapData("buttonDown", "../assets/images/ButtonDown.png")  
    ..addBitmapData("logo", "../assets/images/img1.png")  
    ..addBitmapData("desktop", "../assets/images/desktop.jpg")  
    ..addBitmapData("background", "../assets/images/background.jpg")
    ..addBitmapData("success", "../assets/images/img1_success.png")  
    ..addBitmapData("failed", "../assets/images/img1_failed.png")  
    ..addSound("plop", "../assets/sounds/Plop.mp3"); 
//    ..addTextureAtlas("titan", "../assets/images/walk.json", TextureAtlasFormat.JSONARRAY);  // 可以用TP工具產出
  
  
  // Dart語法: 簡易寫法 但是記得加() 因為編輯器不會發現
  gResourceManager.load().then((_)=>createWorld()); 
  
  
  gbGameState = 1;
  
  
  
  
}



void createWorld() 
{

  // GameOver表示
//  gGameOverTextField = new TextField();
//  gGameOverTextField.defaultTextFormat = new TextFormat("Verdana", 80, 0xFF0000, bold:true, align:TextFormatAlign.CENTER);
//  gGameOverTextField.width = 500;
//  gGameOverTextField.height = 200;
//  gGameOverTextField.text = "Game Over";
//  gGameOverTextField.x = 180;
//  gGameOverTextField.y = 200;
//  gGameOverTextField.mouseEnabled = false;
  
  // 還剩下多少隻
  gTitanCountTextField = new TextField();
  gTitanCountTextField.defaultTextFormat = new TextFormat("Verdana", 20, 0xAA0000, bold:true, align:TextFormatAlign.CENTER);
  gTitanCountTextField.width = 100;
  gTitanCountTextField.height = 50;
  gTitanCountTextField.text = "0/100";
  gTitanCountTextField.x = nScreenWidth - 110;
  gTitanCountTextField.y = 10;
  gTitanCountTextField.mouseEnabled = false;
  
  // 建立LOGO
  gLogo = new Bitmap(gResourceManager.getBitmapData("logo"));  
  gLogo.x = (nScreenWidth - 640)/2;
  gLogo.y = 120;  
  
  // 建立成功LOGO
  gSuccess = new Bitmap(gResourceManager.getBitmapData("success"));  
  gSuccess.x = (nScreenWidth - 640)/2;
  gSuccess.y = 120;  
  
  // 建立失敗LOGO
  gFailed = new Bitmap(gResourceManager.getBitmapData("failed"));  
  gFailed.x = (nScreenWidth - 640)/2;
  gFailed.y = 120;    
  
  // 建立說明
  gCopyrightTextField = new TextField();   
  gCopyrightTextField.defaultTextFormat = new TextFormat("Verdana", 12, 0xFFFFFF, bold:true, align:TextFormatAlign.CENTER);
  gCopyrightTextField.text = "KurtYu (Build 1.0.0)";
  gCopyrightTextField.x = nScreenWidth/2-100;
  gCopyrightTextField.y = nScreenHeight - 40;
  gCopyrightTextField.width = 200;    
  gCopyrightTextField.mouseEnabled = false;  
  
  
  // 結算隻數
  gTitanDefeatTextField = new TextField();   
  gTitanDefeatTextField.defaultTextFormat = new TextFormat("Verdana", 52, 0x990000, bold:true, align:TextFormatAlign.CENTER);
//  gTitanDefeatTextField.text = "100隻擊倒";
  gTitanDefeatTextField.x = nScreenWidth/2-180;
  gTitanDefeatTextField.y = nScreenHeight/2 - 280;
  gTitanDefeatTextField.width = 300;
  gTitanDefeatTextField.borderColor = 0x000000;
  gTitanDefeatTextField.mouseEnabled = false;
  
  // 結算暱稱
  gTitleTextField = new TextField();   
  gTitleTextField.defaultTextFormat = new TextFormat("Verdana", 42, 0xFFFFFF, bold:true, align:TextFormatAlign.CENTER);
//  gTitleTextField.text = "調查兵團畢業生";  
  gTitleTextField.x = nScreenWidth/2-80;
  gTitleTextField.y = nScreenHeight/2 - 200;
  gTitleTextField.width = 300;
  gTitleTextField.height = 50;
  gTitleTextField.background = true;
  gTitleTextField.backgroundColor = 0x0000FF;
  gTitleTextField.borderColor = 0x000000;  
  gTitleTextField.mouseEnabled = false;    

  
  // 建立背景
  gDesktop = new Bitmap(gResourceManager.getBitmapData("desktop"));
  
//  gBackground = new Bitmap(gResourceManager.getBitmapData("background"));
  

  gStage.addChild(gDesktop);  
  gStage.addChild(gLogo);
  gStage.addChild(gCopyrightTextField);  
  

  
  // 加入按鈕
  addButton((nScreenWidth-250)/2, 430);  
    
  
  // 載入Flump產出的動畫檔案
  FlumpLibrary.load("../assets/images/library.json").then(onFlumpLibraryLoaded);
  
}


void addButton(var x, var y)
{
  Bitmap buttonUp = new Bitmap(gResourceManager.getBitmapData("buttonUp"));
  Bitmap buttonOver = new Bitmap(gResourceManager.getBitmapData("buttonOver"));
  Bitmap buttonDown = new Bitmap(gResourceManager.getBitmapData("buttonDown"));

  gStartButton = new SimpleButton(buttonUp, buttonOver, buttonDown, buttonOver);
  gStartButton.x = x;
  gStartButton.y = y;

  gStage.addChild(gStartButton);

  gStartButtonTextField = new TextField();
  gStartButtonTextField.defaultTextFormat = new TextFormat("Verdana", 30, 0xFFFFFF, bold:true, align:TextFormatAlign.CENTER);
  gStartButtonTextField.width = gStartButton.width;
  gStartButtonTextField.height = 40;
  gStartButtonTextField.text = "Attack";
  gStartButtonTextField.x = gStartButton.x;
  gStartButtonTextField.y = gStartButton.y + 10;
  gStartButtonTextField.mouseEnabled = false;

  gStage.addChild(gStartButtonTextField);
  
  

 
 // 加入Listener
  gStartButton.addEventListener(MouseEvent.CLICK, (MouseEvent me)
      {

    print("遊戲開始" + gbGameState.toString());
    
    switch(gbGameState)
    {
      case 1:
      {
        // 移除按鈕
        gStartButton.removeFromParent();
        gStartButtonTextField.removeFromParent();
        
        // 移除背景
        gDesktop.removeFromParent();
        // 移除Logo
        gLogo.removeFromParent();
        gCopyrightTextField.removeFromParent();
        
        // 加入遊戲場景
        gbGameState = 0;
        gTitanCount = 0;
        gDefeatCount = 0;
        
        gTitanCountTextField.text = "0/100";
        
        gGameScene = new GameScene();
        gStage.addChild(gGameScene);
        gbStopCreate = false;
        
        // 加入巨人
        addTitan();    
        
        // 加入巨人計數
        gStage.addChild(gTitanCountTextField);         
      }
      break;
      // 完成
      case 2:
      // 失敗
      case 3:
      {
        gbGameState = 1;
        
        // 移除按鈕
        gStartButton.removeFromParent();
        gStartButtonTextField.removeFromParent();
        gTitanCountTextField.removeFromParent();
        gFailed.removeFromParent();
        gSuccess.removeFromParent(); 
        gGameScene.removeFromParent();     
        gGameScene = null;        
        
        gTitanDefeatTextField.removeFromParent();
        gTitleTextField.removeFromParent();
        
        
        gStage.addChild(gDesktop);
        gStage.addChild(gLogo);
        gStage.addChild(gCopyrightTextField);
        gStartButtonTextField.text = "Attack";
        gStage.addChild(gStartButton);
        gStage.addChild(gStartButtonTextField);
        
        

      }
      break;
    }
    

    
   
    
    });
  
  
}


void addTitan()
{
  if(gbStopCreate)
    return;
  
  print("gTitanCount:"+gTitanCount.toString());
  

  
  if(gTitanCount >= 0 && gTitanCount < 10 )
  {
    gGameScene.addRealTitan(1);    
    gStage.juggler.delayCall(addTitan, 4.0);    
  }
  else if(gTitanCount == 10)
  {
    gGameScene.addRealTitan(2);    
    gStage.juggler.delayCall(addTitan, 3.0);    
  }
  else if(gTitanCount > 10 && gTitanCount < 20)
  {
    gGameScene.addRealTitan(1);    
    gStage.juggler.delayCall(addTitan, 2.5);
    if(gTitanCount == 15)
    {
      gGameScene.addRealTitan(4);  
    }
  }
  else if(gTitanCount == 20)
  {
    gGameScene.addRealTitan(2);    
    gStage.juggler.delayCall(addTitan, 2.5);    
  }    
  else if(gTitanCount > 20 && gTitanCount < 40)
  {
    gGameScene.addRealTitan(1);    
    gStage.juggler.delayCall(addTitan, 2.0);
    if(gTitanCount == 25)
    {
      gGameScene.addRealTitan(4);  
    }    
  }
  else if(gTitanCount == 40)
  {
    gGameScene.addRealTitan(2);    
    gStage.juggler.delayCall(addTitan, 2.0);    
  }  
  else if(gTitanCount > 40 && gTitanCount < 50)
  {
    gGameScene.addRealTitan(1);    
    if(gTitanCount == 45)
    {
      gGameScene.addRealTitan(4);  
    }     
    else if(gTitanCount == 47)
    {
      gGameScene.addRealTitan(2);  
    }
    gStage.juggler.delayCall(addTitan, 1.5);    
  }
  else if(gTitanCount == 50)
  {
    gGameScene.addRealTitan(3);    
    gStage.juggler.delayCall(addTitan, 1.5);    
  }    
  else if(gTitanCount > 50 && gTitanCount < 70)
  {
    gGameScene.addRealTitan(1);    
    if(gTitanCount == 55)
    {
      gGameScene.addRealTitan(4);  
    }     
    else if(gTitanCount == 57)
    {
      gGameScene.addRealTitan(2);  
    }    
    gStage.juggler.delayCall(addTitan, 1.0);    
  }
  else if(gTitanCount == 70)
  {
    gGameScene.addRealTitan(3);    
    gStage.juggler.delayCall(addTitan, 1.0);    
  }     
  // 10隻奇形種
  else if(gTitanCount > 70 && gTitanCount <  80)
  {
    gGameScene.addRealTitan(2);    
    if(gTitanCount == 75)
    {
      gGameScene.addRealTitan(4);  
    }     
    else if(gTitanCount == 77)
    {
      gGameScene.addRealTitan(2);  
    }        
    gStage.juggler.delayCall(addTitan, 1.0);     
  }
  else if(gTitanCount == 80)
  {
    gGameScene.addRealTitan(3);    
    gStage.juggler.delayCall(addTitan, 1.0);    
  }    
  else if(gTitanCount > 80 && gTitanCount < 100)
  {
    gGameScene.addRealTitan(1);
    if(gTitanCount == 85)
    {
      gGameScene.addRealTitan(4);  
    }     
    else if(gTitanCount == 90)
    {
      gGameScene.addRealTitan(2);  
    }  
    else if(gTitanCount == 91)
    {
      gGameScene.addRealTitan(2);  
    }     
    else if(gTitanCount == 92)
    {
      gGameScene.addRealTitan(2);  
    }     
    else if(gTitanCount == 93)
    {
      gGameScene.addRealTitan(2);  
    } 
    else if(gTitanCount == 94)
    {
      gGameScene.addRealTitan(2);  
    }     
    gStage.juggler.delayCall(addTitan, 1.0);    
  }
  else
  {
    // 100隻 不用再加了
    gbStopCreate = true;
  }
  
  gTitanCount++;
  
}



void onFlumpLibraryLoaded(FlumpLibrary flumpLibrary)
{
// do nothing
  gFlumpLibrary = flumpLibrary;
}


